<?php
namespace app\backend\controller;

use app\backend\model\ArticleModel;
use app\backend\model\ProductCategoryModel;
use app\backend\model\NotificationModel;
use app\common\model\Advertisement;
use app\common\model\ArticleCategory;
use app\common\model\Feedback;
use app\common\model\Nav;
use app\common\model\Notification;

class Other extends Base
{
    public function navList()
    {
        $where = [];
        $list = Nav::treeList();
        foreach ($list as &$v) {
            $v['title'] = $v['prefix']. ' ' .$v['title'];
        }
        $this->assign('data', [
            'title' => '导航管理',
            'collection' => $list,
            'thead' => [
                ['#','width:60px'],
                '标题', '路径', '类型','模板文件','参数','GID',
            ],
            'fields' => [
                'id','title','path','page_type','temp_name','params','gid'
            ],
            'buttons' => [
                ['title'=>'修改', 'onclick' => 'showBigWindow', 'url'=> url('createNav')],
                ['title'=>'删除', 'onclick' => 'delOne', 'url'=> url('deleteNav')],
            ],
            'mbuttons' => [
                ['title'=>'添加导航', 'onclick' => 'showBigWindow', 'url'=> url('createNav')],
            ],
        ]);
        return $this->fetch('public/table_builder');
    }
    public function createNav()
    {
        $model = 'app\backend\model\NavModel';
        if(request()->isPost()) {
            $data = input('post.');
            if($id = input('id')) {
                $this->modelUpdate($model, $data, ['id' => $data['id']], 'nav_saving', '修改导航');
            } else {
                $this->modelCreate($model, $data, 'nav_saving', '添加导航');
            }
        }else {
            return $this->fetchFormPageHtml($model, __FUNCTION__);
        }
    }
    public function deleteNav()
    {
        $id = input('id');
        echo Nav::destroy($id) ? 1 : 0;
    }
    public function adList()
    {
        $where = [];

        $this->assign('data', [
            'title' => '广告管理',
            'collection' => Advertisement::where($where)->order('id desc')->paginate(config('paginate.per_page')),
            'thead' => [
                ['#','width:60px'],
                '标题',
                '位置标识GID',
                "链接"
            ],
            'fields' => [
                function($row) {
                    return '<image src="'.$row['image'].'" class="image-in-td">';
                },'title','gid','uri'
            ],
            'buttons' => [
                ['title'=>'修改', 'onclick' => 'showBigWindow', 'url'=> url('createAd')],
                ['title'=>'删除', 'onclick' => 'delOne', 'url'=> url('deleteAd')],
            ],
            'mbuttons' => [
                ['title'=>'添加广告', 'onclick' => 'showBigWindow', 'url'=> url('createAd')],
            ],

        ]);
        return $this->fetch('public/table_builder');
    }
    public function createAd()
    {
        $model = 'app\backend\model\AdModel';
        if(request()->isPost()) {
            $data = input('post.');
            $data['edit_time'] = now();
            $data['image'] = empty($data['image']) ? '' : $data['image'][0];
            if($id = input('id')) {
                $this->modelUpdate($model, $data, ['id' => $data['id']], 'ad_saving', '修改广告');
            } else {
                $data['add_time'] = now();
                $this->modelCreate($model, $data, 'ad_saving', '添加广告');
            }
        }else {
            return $this->fetchFormPageHtml($model, __FUNCTION__);
        }
    }
    public function deleteAd()
    {
        $id = input('id');
        echo Nav::destroy($id) ? 1 : 0;
    }
}
